//
//  DoubleExtensions.swift
//  clreader
//
//  Created by colin on 2019/4/3.
//  Copyright © 2019 colin. All rights reserved.
//

extension Double {
    
    var priceAttr:NSAttributedString? {
        if self < 0 { return nil }
        
        let priceString = String(self)
        let attr = NSMutableAttributedString()
        
        if self == 0 {
            attr.append(NSAttributedString(string: "免费", attributes: [.font:UIFont.systemFont(ofSize: 12.0, weight: .light),.foregroundColor:UIColor(hex: "AE1600")]))
            return attr
        }
        
        attr.append(NSAttributedString(string: "¥", attributes: [.font:UIFont.systemFont(ofSize: 12.0, weight: .light),.foregroundColor:UIColor(hex: "343434")]))
        if let index = priceString.firstIndex(of: ".") {
            let intString = String(priceString[priceString.startIndex ..< index])
            attr.append(NSAttributedString(string: intString, attributes: [.font:UIFont.systemFont(ofSize: 17.0, weight: .bold),.foregroundColor:UIColor(hex: "343434")]))
            let floatString = String(priceString[index ..< priceString.endIndex])
            attr.append(NSAttributedString(string: floatString, attributes: [.font:UIFont.systemFont(ofSize: 12.0, weight: .light),.foregroundColor:UIColor(hex: "343434")]))
        } else {
            attr.append(NSAttributedString(string: priceString, attributes: [.font:UIFont.systemFont(ofSize: 17.0, weight: .bold),.foregroundColor:UIColor(hex: "343434")]))
        }
        return attr
    }
    
    
}
